package com.candy.candylog.business.controller;

import com.candy.candycommon.utils.ApplicationContentUtils;
import com.candy.candycommon.utils.BeanConvertUtils;
import com.candy.candycommon.utils.ResultBean;
import com.candy.candylog.business.entity.LogLink;
import com.candy.candylog.business.service.ILogLinkService;
import com.candy.candylog.event.LogEvent;
import com.candy.candylogservice.dto.LogLinkDTO;
import com.candy.candylogservice.interceptor.LogUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author suyanlong
 * @since 2022-10-25
 */
@Slf4j
@RestController
@RequestMapping("/logLink")
@AllArgsConstructor
public class LogLinkController {

    private final ILogLinkService iLogLinkService;

    @PostMapping("save")
    public ResultBean save(@RequestBody LogLinkDTO logLinkDTO) {
        log.info("保存日志" + LogUtils.getTraceId());

        ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) ApplicationContentUtils.getApplicationContext().getBean("asyncTaskExecutor");
        executor.execute(() -> {
            LogLink logLink = BeanConvertUtils.convertTo(logLinkDTO, LogLink::new);
            log.info("发布日志事件");
            ApplicationContentUtils.getApplicationContext().publishEvent(new LogEvent.LogLinkEvent("logLink", logLink));
        });

        return ResultBean.ok(logLinkDTO);
    }

}
